from collections import ChainMap, Counter, OrderedDict, defaultdict, deque
from utils.test_tools import test_decorator


@test_decorator('collection.ChainMap')
def test_chain_map():
    """
    1、ChainMap
        将多个字典合并为一个字典映射，比字典的update()方法效率高
        内置: new_child() parent() 和 常用字典方法: items() keys() values() pop() get()
    """

    # 定义多个字典
    dict_a = {'name': 'Mr_52Hz', 'gender': 'man', 'age': 18}
    dict_b = {'city': 'beijing', 'work': 'programmer'}

    # 合并
    all_msg_dic = ChainMap(dict_a, dict_b)


    # 属性输出
    print(dict_a['name'], all_msg_dic['name'])

    # ChainMap修改字典值
    all_msg_dic['age'] = 20
    print(dict_a['age'], all_msg_dic['age'])

    # 扩展字典
    dict_c = {'degree': 1, 'score': 1000, 'experience': 5000}
    # all_msg_dic.maps += [dict_c]
    all_msg_dic = all_msg_dic.new_child(dict_c)  # 注意要有返回值接收
    print(all_msg_dic)
    print(all_msg_dic['degree'])
    # 查看上一级的all_msg_dic
    print(all_msg_dic.parents)
    all_msg_dic = all_msg_dic.new_child()
    print(all_msg_dic.parents)

    # for key, value in all_msg_dic.items():
    #     print(key, value)


@test_decorator('collection.Counter')
def test_counter():
    """
    2、Counter
        对象计数器
        内置: most_common() subtract() update()
    """

    # 初始化计数器
    counter = Counter()
    counter1 = Counter({'a': 2, 'b': 3})
    counter2 = Counter(a=3, b=1, c=8)

    # 计数器更新
    counter.update('abcdefg')
    counter1.update(['a', 'b', 'c', 'd'])
    counter2.update(counter1)
    counter2.subtract(counter)
    most = counter2.most_common(3)

    print(counter)
    print(counter1)
    print(counter2.keys(), counter2.values())
    print(most)


@test_decorator('collection.OrderDict')
def test_ordered_dict():
    """
    3、OrderedDict
        记录字典键值的插入顺序
        'clear', 'copy', 'get', 'keys', 'move_to_end', 'pop', 'popitem', 'update'
    """
    dict_a = OrderedDict()
    dict_a['name'] = 'Mr_52Hz'
    dict_a['age'] = 19
    dict_a['gender'] = 'man'
    print(dict_a)
    dict_a.move_to_end('name')
    print(dict_a)


@test_decorator('collection.defaultdict')
def test_default_dict():
    s = 'abcddefasafsdfa'
    dict_a = defaultdict(int)
    for i in s:
        dict_a[i] += 1

    msg = [('laowang', 'man'), ('sunxiao', 'wom'), ('jiangning', 'wom'), ('fuqixiaong', 'man')]
    dict_b = defaultdict(list)
    for name, gender in msg:
        dict_b['name'].append(name)
        dict_b['gender'].append(gender)

    print(dict_b)
    print(dict_a)


@test_decorator('collection.deque')
def test_deque():
    """
    5、deque
        maxlen <int> 参数指定队列长度，先进先出
    双边队列
    内置方法：append', 'appendleft', 'clear', 'copy', 'count', 'extend', 'extendleft',
            'index', 'insert', 'maxlen', 'pop', 'popleft', 'remove', 'reverse', 'rotate'
    """
    # print(deque.__doc__)
    # print(dir(deque))
    ls_d = deque()
    ls_d.append(1)
    print(ls_d)
    ls_d.appendleft(2)
    print(ls_d)
    ls_d.extend([4, 5])
    print(ls_d)
    ls_d.extendleft([9, 8])
    print(ls_d)
    # print(ls_d.pop())
    # print(ls_d.popleft())
    ls_d.rotate(2)
    print(ls_d)
    ls_d.rotate(-2)
    print(ls_d)

    # 主要用于 记录最近几次的数据
    deque_len_3 = deque(maxlen=3)

    deque_len_3.append(1)
    print(deque_len_3)
    deque_len_3.append(2)
    print(deque_len_3)
    deque_len_3.append(4)
    print(deque_len_3)
    deque_len_3.append(5)
    print(deque_len_3)
    print(deque_len_3[0])


if __name__ == '__main__':
    test_chain_map()
    test_counter()
    test_ordered_dict()
    test_default_dict()
    test_deque()



